home *** CD-ROM | disk | FTP | other *** search
- Path: gambier.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: need for function prototypes
- Date: 5 Mar 1996 13:56:10 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hid9qINNapq@gambier.ugrad.cs.ubc.ca>
- References: <4gutho$o1a@mn5.swip.net> <4hgbo0$2fj@s02.pavilion.co.uk> <4hi462$3bf@ams.amsinc.com>
- NNTP-Posting-Host: gambier.ugrad.cs.ubc.ca
-
- In article <4hi462$3bf@ams.amsinc.com>,
- Steve Herborn <sherborn@mail.amsinc.com> wrote:
- >ANSI also introduced void as meaning that the function takes no
- >arguments, thus removing another source of confusion.
-
- That actually introduces a little bit of confusion. The void marker for an
- empty paramenter list is just there because the grammar for the parameter list
- does not allow it to consist of empty tokens. If you omit the void, it becomes
- an old-style declaration.
-
- The grammar productions for direct-declarator are as follows (from K&R A13):
-
- direct-declarator:
- identifier
- ( declarator )
- direct-declarator [ constant-expression_opt ]
- direct-declarator ( parameter-type-list )
- direct-declarator ( identifier-list_opt )
-
- There is nothing in the further productions for parameter-type-list that would
- allow an empty derivation. However, the identifier-list_opt is clearly
- optional, hence it is allowed to derive an empty string.
-
- Now, if both were allowed to derive empty strings, the grammar would be
- ambiguous. If the parser saw a declaration of int foo(), it would not know
- whether it is new style (production 3 above) or old style (production 4).
-
- This matters, because the old-style declaration with no parameter list is just
- a function declaration which can stand for a definition that has arguments.
- The new-style declaration, on the other hand, is a prototype which asserts that
- the parameter list is to be empty.
-
- The ``void'' keyword by itself is just a syntactic kludge do distinguish
- between the old and the new.
-
- To see that it is a kludge, try defining a function:
-
- int foo(int) { }
-
-
- This is syntactically equivalent to writing int foo(void), since both int and
- void play the syntactic role of ``type-specifier'', and the declaration
- specifiers that follow the type specifier are optional.
-
- However, the above is not allowed in a function _definition_, since the int
- parameter is not named. However, the void parameter is yet allowed to be
- without a name.
-
- This is the ``syntactic ugliness'' referred to in the K&R2 at the end of
- A8.6.3.
- --
-
-